package com.ikkong.projects.controller;

import java.io.File;
import java.util.*;

import com.ikkong.common.vo.User;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.platform.model.Expert;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.projects.model.Projects;
import com.ikkong.projects.service.ProjectsService;
import com.ikkong.projects.service.impl.ProjectsServiceImpl;
import com.ikkong.system.model.Dept;
import com.jfinal.kit.PathKit;
import com.jfinal.template.expr.ast.Array;
import com.jfinal.upload.UploadFile;
import org.apache.poi.ss.usermodel.DateUtil;

import javax.servlet.http.HttpServletRequest;

import static com.ikkong.myproject.controller.MyprojectController.FILE_NAMES;

/**
 * Generated by Blade.
 * 2018-03-27 10:43:30
 */
public class ProjectsController extends UrlPermissController {
	private static String CODE = "projects";
	private static String PERFIX = "projects";
	private static String LIST_SOURCE = "Projects.list";
	private static String BASE_PATH = "/platform/projects/";
	private static String ENJOY_PATH="/WEB-INF/view/platform/";
	
	ProjectsService service = new ProjectsServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "projects.html");
	}

	public void add() {
        FILE_NAMES = "";
        Date d = new Date();
        Integer year = com.ikkong.system.util.DateUtil.getYear(d);
        int year1 = year + 1;
        setAttr("year",year);
        setAttr("yea",year1);
        setAttr("code", CODE);
		String deptName = ShiroKit.getUser().getDeptName().toString();
		String deptId = ShiroKit.getUser().getDeptId().toString();
		setAttr("deptId",deptId);
		setAttr("deptName",deptName);
		render(BASE_PATH + "projects_add.html");
	}
	/*
	*跳转到编辑页面
	 */
	public void edit() {
		FILE_NAMES = "";
		String id = getPara(0);
		Projects projects = service.findById(id);
		setAttr("projects", projects);
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "projects_nopass_1.html");
	}
	public void view() {

		setAttr("code", CODE);
		String id = getPara("id");
		Projects projects = service.findById(id);
		if(projects != null){
            setAttr("projects",projects);

            //建设方案
            if(projects.getPlan_file()!=null && projects.getPlan_file()!=""){
                String[] files = projects.getPlan_file().split(",");
                List<String> plan_files = Arrays.asList(files);
                setAttr("plan_files",plan_files);
            }
            //附件
            String annex = projects.getAnnex();
			if(annex !=null) {
                String[] annexs = annex.split(",");
                List<String> strings = Arrays.asList(annexs);
                setAttr("annexs", strings);
            }

            //专家意见
            if(projects.getExpert_opinion() != "" && projects.getExpert_opinion()!=null){
                setAttr("expert_opinion",projects.getExpert_opinion());
            }

            //会商前意见
            if(projects.getTohs_opinion()!=null && projects.getTohs_opinion()!=""){
                setAttr("tohs_opinion",projects.getTohs_opinion());
            }
            //会商意见
            if(projects.getHs_opinion()!=null && projects.getHs_opinion()!=""){
                setAttr("hs_opinion",projects.getHs_opinion());
            }

				render(BASE_PATH + "projects_view.html");
		}
	}

	public void list() {
		String deptid = ShiroKit.getUser().getDeptId().toString();
		Map map = new HashMap();
		map.put("deptid",deptid);
		List select = Blade.dao().select(LIST_SOURCE, Projects.class, map);
		renderJson(select);
	}
	public void mylist() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

    public void toplan(){
        setAttr("code",CODE);
        render(BASE_PATH    +  "toplan.html");
    }
    public void output(){
        renderText("output");
    }
    public void reout(){
        renderText("reout");
    }
    public void planli(){
        Object grid = paginate("Projects.planlist");
        renderJson(grid);
    }
	public void save() {
        Projects projects = mapping(PERFIX, Projects.class);
		boolean temp = service.save(projects);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}
	/*
	*跳转到初审意见填写页面
	 */
	public void back() {
		Integer id= Integer.parseInt(getPara("id"));
		Projects pro = service.findById(id);
		Integer status = pro.getStatus();
		if(status == 13){
			setAttr("pro",pro);
			render(BASE_PATH + "../listcom/review_opinion.html");
		}else {
			setAttr("pro",pro);
			render(BASE_PATH + "projects_audit_opinion.html");
		}


	}

	/*
	*初审不通过，提交初审意见
	 */
	public void backToUser() {

		Integer id= Integer.parseInt(getPara("id"));
        Projects pro = service.findById(id);
        Integer sta = pro.getStatus();
        String opinion = getPara("opinion");
		Projects p = new Projects();
		p.setId(id);
		p.setAudit_opinion(opinion);
        if(sta == 0 || sta ==8){
            p.setStatus(1);
        }else if(sta==3 ||sta==7){
            p.setStatus(2);
        }

		boolean temp = service.update(p);
		if (temp) {
            setAttr("href","/listcom/index");
            setAttr("msg","秒后跳转到项目列表");
			render(BASE_PATH+"success.html");
		} else {
			renderJson(error("审核失败!请联系管理员"));
		}
	}
	/*
	*选择专家提交至后台
	 */
	public void  addExpert() {
		int pid = Integer.parseInt(getPara("id"));
		Date lastdate = getParaToDate("lastdate");
		String[] ids = getParaValues("select");
		String id = "";
		for (int i = 0; i < ids.length; i++) {
			id = id + ids[i] + ",";
		}
		Projects p = new Projects();
		p.setId(pid);
		p.setChoose_date(new Date());
		p.setFeedback(lastdate);
		p.setExpert_ids(id);
		p.setStatus(6);//已选择专家
		boolean tem = service.update(p);
		if(tem){
			setAttr("href","/listcom/index");
			setAttr("msg","秒后跳转到审核列表");
			render(BASE_PATH + "success.html");
		}
	}
	/*
	跳转到选择专家页面
	 */
	public void chooseExpert() {
        String has = getPara("has");
        Integer id = Integer.parseInt(getPara("id"));

        Projects p = service.findById(id);
        if("1".equals(has)){
            p.setId(id);
            p.setStatus(5);//已选择专家
        }
		service.update(p);
		Dept dept = Blade.create(Dept.class).findById(p.getDept_id());
		List<Object> experts = Blade.create(Expert.class).findAll();
		if(experts.size()>0){
			setAttr("experts",experts);
		}
		if(dept == null){
			setAttr("dept","未找到单位");
		}else {
			setAttr("dept",dept.getFullname());
		}
        setAttr("has",has);
		setAttr("project",p);
		render(BASE_PATH + "projects_pass.html");
	}
	/*
	初审通过
	 */
	public void auditOk() {
		Integer id= Integer.parseInt(getPara("id"));
		Projects p = new Projects();
		p.setId(id);
		p.setStatus(2);
		p.setAudit_opinion("已通过初审");
		boolean temp = service.update(p);
		if (temp) {
			p=service.findById(id);
			Dept dept = Blade.create(Dept.class).findById(p.getDept_id());
			List<Object> experts = Blade.create(Expert.class).findAll();
			if(experts.size()>0){
				setAttr("experts",experts);
			}
			if(dept == null){
				setAttr("dept","未找到单位");
			}else {
				setAttr("dept",dept.getFullname());
			}

			setAttr("project",p);
			setAttr("href","/listcom/index");
			setAttr("msg","秒后跳转到审核列表");

			render(BASE_PATH + "success.html");
		} else {
			renderJson(error("审核失败!"));
		}
	}

	public void update() {
		Projects projects = getBean(Projects.class);
		boolean temp = service.update(projects);
		if (temp) {
			setAttr("href","/myproject/index");
			setAttr("msg","即将跳转到我的项目列表");
			render(BASE_PATH +"success.html");
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}
	/*
	* 项目流程
	* */
	public void process(){
		int id = Integer.parseInt(getPara("id"));
		Projects pro = service.findById(id);
		setAttr("projects",pro);
		render(BASE_PATH + "projects_process.html");
	}
	/*
		跳转到上传建设方案页面
	 */
	public void scheme() {
		String id = getPara("id");
		setAttr("id",id);
		render(BASE_PATH + "projects_upload_scheme.html");
	}

	/*
		接收建设方案文件上传
	 */
	public void uploadScheme(){
        Integer id = getParaToInt("id");
            String fileName = getPara("liuxiaobo");
            Projects projects = new Projects();
			projects.setId(id);
			projects.setPlan_file(fileName);
			projects.setStatus(3);//上传方案已提交，等待经信委复审
			service.update(projects);
			setAttr("projects",service.findById(id));
			setAttr("href","/myproject/index");
			setAttr("msg","2秒后跳转到我的项目");
			render(BASE_PATH + "success.html");
        //上传多文件
//        String filename = "";
//        List<UploadFile> upFiles = getFiles();//批量上传文件
//        for (UploadFile fileItem : upFiles) {
//                filename = filename+","+fileItem.getFileName();
//            }

    }
	/*
		专家审核项目列表
	 */
	public  void  byExpert(){
		String expertId = ShiroKit.getUser().getId().toString();
		List<Projects> projectses = service.findAll();
		List<Projects> byExpert = new ArrayList<>();
		Projects p = new Projects();
		for(int i = 0;i<projectses.size();i++){
			p = projectses.get(i);
			String experts = p.getExpert_ids();

			if(experts !=null){
				String[] ids = experts.split(",");
					for(int j = 0;j<ids.length;j++){
						if(expertId.equals(ids[j])){
							byExpert.add(p);
							break;
						}
					}

			}
		}
		renderJson(byExpert);
	}
    public  void downs(){
        String file = getPara("file");
        renderFile(file);
    }
	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
